﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarProduction : MonoBehaviour {

    public Wave carWave;
    public Transform startPosition;
    private Coroutine coroutine;
    public float waveTime = 2f;
    [SerializeField]
    public static int carAliveCount=0;
    public Animator wave;

    public void OnWaveDown()
    {
        wave.SetTrigger("Down");
        coroutine = StartCoroutine(CarProduct());
    }

    IEnumerator CarProduct()
    {
        int count = Random.Range(0, carWave.Count1);
       
            for(int j=0;j<count;j++)
            {
                Vector3 position = new Vector3(startPosition.position.x, startPosition.position.y, 0);
                GameObject.Instantiate(carWave.Car1,position,Quaternion.identity);
                if (j != count - 1)
                    yield return new WaitForSeconds(carWave.timeGap);
                carAliveCount++;
            }
            yield return new WaitForSeconds(waveTime);
        int count2 = Random.Range(0, carWave.Count2);
        for (int j = 0; j < count2; j++)
            {
                Vector3 position = new Vector3(startPosition.position.x, startPosition.position.y, 0);
                GameObject.Instantiate(carWave.Car2, position, Quaternion.identity);
                if (j != count2 - 1)
                    yield return new WaitForSeconds(carWave.timeGap);
                carAliveCount++;
            }

           
    }
    public void Stop()
    {
        StopCoroutine(coroutine);
    }
}
